package com.baiyun.stock.mq;


import com.baiyun.stock.service.StockService;
import com.github.benmanes.caffeine.cache.Cache;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@RequiredArgsConstructor
@Slf4j
/**
 * 监听消息类
 */
public class StockMQMsgListener {
    private final StockService stockService;
    private final Cache caffenineCache;

    /**
     * 保证数据的一致性
     * 监听采集内盘信息，采集完毕后更新缓存
     * @param startTime
     */
    @RabbitListener(queues = "innerMarketQueue")
    public void refreshInnerMarketInfo(Date startTime) {
        //判断延时是否超过一分钟
        long delay = System.currentTimeMillis() - new DateTime(startTime).getMillis();
        if (delay > 60000L) {
            log.error("延时大于1分钟警告,延时为{}ms",delay);
        }
        //刷新缓存，先删除旧缓存，再从数据库加载数据到缓存
        caffenineCache.invalidate("innerMarket");
        stockService.innerIndexAll();
    }
}
